<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
?>

<style>
    select{
        width: auto;
    }
</style>
<div id="ax_form_result">
    <?php // echo ($showtimeItem->title() != "") ? $showtimeItem->title() : '' ?>
</div>
<input type="hidden" name="base_url" id="base_url" value="<?php echo base_url() ?>" />

<article class="module width_full">
	<form method="POST" id="acx_form"
		action="<?php // echo $showtimeItem->item_id() ? site_url("showtime/form") : site_url("showtime/form") ?>">
		<header>
			<h3>
                            <?php echo $movieItem->title(); ?>
                            <img src="<?php echo base_url(); ?>/assets/css/_common/images/loadmorespinner.gif" id="imageloadmore" />
                        </h3>
		</header>
		<div class="module_content">
			<input type="hidden" name="movie_id" id="movie_id"
				value='<?php echo (($movieItem->item_id() != "") ? $movieItem->item_id() : ""); ?>' />
                        <fieldset style="width: 100%; margin-right: 1%;">
                                    <label>Tên Phim</label>
                                    <div class="clear"></div>
                                    <div style="margin-left: 10px;">
                                        <?php echo ($movieItem->title() != "") ? $movieItem->title() : '' ?>
                                    </div>
                                    <input type="hidden" name="movie_name" id="movie_name"
                                    value="<?php echo ($movieItem->title() != "") ? $movieItem->title() : '' ?>" />
                        </fieldset>	
                        <div style="width: 25%;">
                            <fieldset style="width: 100%; min-height: 250px; float: left; margin-right: 1%;">
                                    <label>Danh sách rạp</label>
                                    <span id="cinema_status" style="padding-top: 30px; color: red">
                                        (*) Chọn rạp chiếu
                                    </span>
                                    <div class="clear" style="padding: 0 10px">
                                        <input type="hidden" id="cinema_id" value="<?php echo isset($_GET['cinema_id']) ? $cinema_id = $_GET['cinema_id'] : $cinema_id = ''?>"/>                                                                                    
                                            <div id="show_list_cinema">                                
                                            </div>    
                                    </div>                                        
                                    <input type="hidden" name="cinema_name" id="cinema_name"
                                            value="<?php // echo ($cinema->cinema_name() != "") ? $cinema->cinema_name() : '' ?>" />
                                    <input type="hidden" name="cinema_website" id="cinema_website"
                                            value="<?php // echo ($cinema->website() != "") ? $cinema->website() : '' ?>" />
                                    <input type="hidden" name="cinema_mobile" id="cinema_mobile"
                                            value="<?php // echo ($cinema->mobile() != "") ? $cinema->mobile() : '' ?>" />
                                    <input type="hidden" name="lat" id="lat"
                                                value="<?php // echo ($cinema->address()->latitude() != "") ? $cinema->address()->latitude() : '' ?>" />                                    
                                    <input type="hidden" name="log" id="log"
                                                value="<?php // echo ($cinema->address()->longitude() != "") ? $cinema->address()->longitude() : '' ?>" />                                    
                            </fieldset>                             
                        </div>
                        
                        <div style="width: 70%; float: left; margin-left: 3%"> 
                            <span id="movie_format" hidden="hidden">
                                <fieldset style="width: 60%; float: left;">
                                    <label>Thể loại</label>
                                    <div class="clear" style="width: 100%; padding: 5px" id="format_type" hidden="hidden">                                
                                        <div class="button_format_type">
                                            <div id="button_2D" class="button_format_type_bar" onclick="show_movie_showtime('2D');">
                                                2D
                                            </div>
                                            <div id="button_3D" class="button_format_type_bar" onclick="show_movie_showtime('3D')">
                                                3D
                                            </div>
                                            <div id="button_4D" class="button_format_type_bar" onclick="show_movie_showtime('4D')">
                                                4D
                                            </div>
                                            <div id="button_5D" class="button_format_type_bar" onclick="show_movie_showtime('5D')">
                                                5D
                                            </div>
                                            <div class="clear"></div>
                                        </div>
                                        <input type="hidden" id="movie_format_type" name="movie_format" value=""> 
                                    </div>   
                                </fieldset>
                                
                                <fieldset style="width: 35%; float: right; margin-right: 1%;">
                                <label>Thuyết minh</label><div class="clear"></div>                          
                                <input type="radio" name="dubbed" value="1">Lồng tiếng<br>
                                <input type="radio" name="dubbed" value="0">Khác<br>
                                </fieldset>
                            </span>
                            <div class="clear"></div>
                            
                            <fieldset style="width: 100%; margin: 1%;" id="showtime" hidden="hidden">
                                <table class="tablesorter" cellspacing="0">
                                    <thead>
                                        <tr>
                                            <th style="width: 30%">Ngày chiếu</th>
                                            <th>Giờ chiếu</th>                                                                               
                                        </tr>
                                    </thead>

                                    <tbody class="sortable" id="content" >
                                    </tbody>
                                </table>
                            </fieldset>   
                        
                            <div id="add_timestamp" hidden="hidden">
                                <script>
                                    $(function($) {
                                        $("#datetimestamp").datepicker();                                      
                                    });                                 
                                </script>                            
                                <fieldset style="width: 48%; float: left; margin-right: 1%;">
                                        <label>Chọn ngày chiếu</label> 
                                        <div class="clear" style="padding: 0 10px;">
                                            <input type="text" style="width:90%;" id="datetimestamp" class="datetimestamp" 
                                                name="datetimestamp">
                                        </div>	                                                                           			
                                </fieldset> 

                                <fieldset style="width: 48%; float: right; margin-right: 1%;">
                                        <label>Nhập giờ</label> 
                                        <div class="clear" style="padding: 0 10px;">
                                            <input type="text" style="width:90%;" id="timestamp" class="tagsManager input-medium tm-input tm-input-success" 
                                                name="timestamp_list" placeholder="Nhập timestamp rồi ấn enter" >
                                        </div>	                                                                          			
                                </fieldset>
                            </div>
                            
                            <fieldset id="add_link_crawl_movieshowtime" hidden="hidden">                                
				<label>Link crawl lịch chiếu phim</label>
				<div class="clear" style="padding: 2px 10px">                                                               
                                    <a href="#" class="add_link_crawl_showtime">
                                        +Link crawl lịch chiếu phim
                                    </a>  
                                    <h2>Danh sách các link</h2> 
                                    <table style="width: 100%" id="link_crawl_movie_showtime" cellspacing="0" hidden="hidden">
                                        <thead>
                                            <tr>                                                
                                                <th style="width: 10%">STT</th>
                                                <th style="width: 70%">Link phim</th>
                                                <th style="width: 15%">Xử lý</th>                                                                                                                                                                                     
                                            </tr>                                                                                       
                                        </thead>

                                        <tbody id="link_showtime_content">

                                        </tbody>
                                    </table>   
                                </div>
                            </fieldset>
                            
                        </div>                        
			<div class="clear"></div>
			<footer>
				<div class="submit_link">
					<span class="loading">&nbsp;</span> 
                                        <input type="submit" value="Lưu lại" class="alt_btn"> <input
						type="reset" value="Nhập lại" onclick="reset_crawler()">
				</div>
			</footer>
		</div>
	</form>       
</article>

<!--Form Add link Crawl Movie Showtime-->
<div id="form_crawl_showtime" title="Nhập link crawl lịch chiếu phim">
	<form method="POST" enctype="multipart/form-data"
		action="<?php echo site_url('showtime/ajax_crawl_movie_showtime') ?>"
		id="form_movie_showtime">

		<input type="hidden" name="cinema_id" id="crawl_cinema_id"
			value=""/>
                <input type="hidden" name="movie_id" id="crawl_movie_id"
                       value="<?php echo ($movieItem->item_id() != "") ? $movieItem->item_id() : ""?>">
                
                <fieldset style="width: 100%; float: left;">
                        <label>Nhập link crawl</label>
                        <input type="text" style="width: 92%;" name="movie_showtime_url[]">
                </fieldset>
                
                
		<div id="acx_photo_result" style="text-align: center"></div>
	</form>
	<div align="center" id="waiting">
		<span class="loading">&nbsp;</span>
	</div>               
</div>


<script>
    //Hiển thị danh sách các movie khi truy cập trang index
    $(document).ready(function() {
        $('#tag_note').hide();
        $('#imageloadmore').hide();        
        $.ajax({
            url: $('#base_url').val() + "showtime/ajax_get_list_cinema",
            type: "POST",
            data: {},
            dataType: 'json',
            success: function(msg) {                
                insert_cinema_list(jQuery.parseJSON(msg.data));
                $("#imageloadmore").hide();
            }
        });
    });
    
    function insert_cinema_list(jsonArray){   
        console.log(jsonArray);
        if(jsonArray != ''){
            var i;
            var url = $('#base_url').val();             
            console.log(jsonArray.length);            
            if(jsonArray.length == 0){
                data += '<tr>';
                data += '<td colspan="5">Không có rạp</td>';
                data += '</tr>';
                $("#imageloadmore").hide();
            }                                   
            else{
                data = '';
                $.each( jsonArray, function( state, cinema_list) {
                    data += '<h3>' + state + '</h3>';
                    
                    for(i=0; i < cinema_list.length; i++){                    
                        cinema = jQuery.parseJSON(cinema_list[i]);
                        console.log(cinema);                                           
                        data += '<input type="radio" onclick="radio_checked()" name="cinema_id" value="'+ cinema.cinema_id + '"';
                        if(cinema.cinema_id == $("#cinema_id").val()){
                            data += '"checked"' + '/>' 
                        }else{
                            data += ' />';
                        }
                        data += cinema.cinema_name + "</br>";
                                                
                        data += '<input type="hidden" id="name_' + cinema.cinema_id + '" value="' + cinema.cinema_name + '"';
                        if(cinema.cinema_id == $("#cinema_id").val()){
                            data += '"selected"' + '/>' 
                        }else{
                            data += ' />';
                        } 
                        
                        data += '<input type="hidden" id="website' + cinema.cinema_id + '" value="' + cinema.cinema_name + '"';
                        if(cinema.cinema_id == $("#cinema_id").val()){
                            data += '"selected"' + '/>' 
                        }else{
                            data += ' />';
                        } 
                        
                        data += '<input type="hidden" id="mobile_' + cinema.cinema_id + '" value="' + cinema.cinema_name + '"';
                        if(cinema.cinema_id == $("#cinema_id").val()){
                            data += '"selected"' + '/>' 
                        }else{
                            data += ' />';
                        } 
                        
                        data += '<input type="hidden" id="lat_' + cinema.cinema_id + '" value="' + cinema.cinema_name + '"';
                        if(cinema.cinema_id == $("#cinema_id").val()){
                            data += '"selected"' + '/>' 
                        }else{
                            data += ' />';
                        } 
                        
                        data += '<input type="hidden" id="log_' + cinema.cinema_id + '" value="' + cinema.cinema_name + '"';
                        if(cinema.cinema_id == $("#cinema_id").val()){
                            data += '"selected"' + '/>' 
                        }else{
                            data += ' />';
                        }                                                 
                    }
                });
                               
                $("#show_list_cinema").html(data);                   
            }
        }
    }
    
    $("#imageloadmore").hide();
    $("#add_timestamp").hide();
    $("#add_link_crawl_movieshowtime").hide();   
    if($('#cinema_id').val() != ''){        
        $("#imageloadmore").show();
        movie_id = $("#movie_id").val();
        cinema_id = $("#cinema_id").val();
        $("#movie_format").show();
        $("#showtime").show();
        $("#add_timestamp").show();
        $("#add_link_crawl_movieshowtime").show();
        $("#cinema_status").hide();
        $.ajax({
            url: $('#base_url').val() + "showtime/ajax_get_list_showtime",
            type: "POST",
            data: {movie_id: movie_id, cinema_id: cinema_id},
            success: function (msg) {                   
                insert_showtime_list(msg.message.showtime);  
                insert_crawl_uri_list(msg.message.crawl_url);
                $("#imageloadmore").hide();
            }
        });        
    }
    
    function radio_checked(){                     
        $("#movie_format").show();
        $("#add_link_crawl_movieshowtime").show();
        show_movie_showtime();        
    };
    
    function show_movie_showtime(type){         
        if($("input[name=dubbed]:checked").val() != undefined){
            $("#button_2D").removeClass("active");
            $("#button_3D").removeClass("active");
            $("#button_4D").removeClass("active");
            $("#button_5D").removeClass("active");
            $(this).addClass('active');
                       
            $("#movie_format_type").val(type);       

            $('#cinema_name').val($('#name_'+$("input:checked").val()).val());    //Set cinema name        
            $('#cinema_website').val($('#website_'+$("input:checked").val()).val());    //Set cinema_website name        
            $('#cinema_mobile').val($('#mobile_'+$("input:checked").val()).val());    //Set cinema mobile     
            $('#lat').val($('#lat_'+$("input:checked").val()).val());    //Set cinema latitude 
            $('#log').val($('#log_'+$("input:checked").val()).val());    //Set cinema longtitude   

            $("#imageloadmore").show();
            var movie_id = $("#movie_id").val();
            var cinema_id = $("input:checked").val();
            $("#showtime").show();
            $("#add_timestamp").show();
            $("#cinema_status").hide();

            var dubbed = $("input[name=dubbed]:checked").val();
            
            if(type == "2D"){
                var movie_format = 0;
            }
            if(type == "3D"){
                var movie_format = 1;
            }
            if(type == "4D"){
                var movie_format = 2;
            }
            if(type == "5D"){
                var movie_format = 3;
            }

            $.ajax({
                url: $('#base_url').val() + "showtime/ajax_get_list_showtime",
                type: "POST",
                data: {movie_id: movie_id, cinema_id: cinema_id},
                success: function (msg) {                   
                    console.log(msg);
                    insert_showtime_list(msg.message.showtime, movie_format, dubbed);
                    insert_crawl_uri_list(msg.message.crawl_url);
                    $("#imageloadmore").hide();
                }
            }); 
            var d = document.getElementById("button_" + type);
            d.className = d.className + " active";            
        }else{
            Alert("Chọn hình thức thuyết minh");
        }
        
    }
    
//    $("input[type=radio]").on("click", function() {
//        $('#cinema_name').val($('#name_'+$("input:checked").val()).val());    //Set cinema name        
//        $('#cinema_website').val($('#website_'+$("input:checked").val()).val());    //Set cinema_website name        
//        $('#cinema_mobile').val($('#mobile_'+$("input:checked").val()).val());    //Set cinema mobile     
//        $('#lat').val($('#lat_'+$("input:checked").val()).val());    //Set cinema latitude 
//        $('#log').val($('#log_'+$("input:checked").val()).val());    //Set cinema longtitude        
//               
//        $("#imageloadmore").show();
//        movie_id = $("#movie_id").val();
//        cinema_id = $("input:checked").val();
//        $("#showtime").show();
//        $("#add_timestamp").show();
//        $("#add_link_crawl_movieshowtime").show();
//        $("#cinema_status").hide();
//        $.ajax({
//            url: $('#base_url').val() + "showtime/ajax_get_list_showtime",
//            type: "POST",
//            data: {movie_id: movie_id, cinema_id: cinema_id},
//            success: function (msg) {                   
//                console.log(msg);
//                insert_showtime_list(msg.message.showtime);
//                insert_crawl_uri_list(msg.message.crawl_url);
//                $("#imageloadmore").hide();
//            }
//        });
//    });
    
    jQuery("#timestamp").tagsManager({          
        typeahead: true,
        typeaheadAjaxPolling: true,     
        CapitalizeFirstLetter: true,
        preventSubmitOnEnter: true,  
        delimeters: [188, 13, 9],
        blinkBGColor_1: '#FFFF9C',
        blinkBGColor_2: '#CDE69C',
        hiddenTagListName: 'timestamp_list',
        hiddenTagListId: 'timestamp_list'            
    });        

    function get_crawl_url(url){
        
    }
    
    /*------------------------------Insert showtime--------------------------*/
    function insert_showtime_list(jsonArray, movie_format, dubbed){ 
        console.log(jsonArray);        
        var time = new Date();        
        
        var i , j;              
        if(jsonArray != null){
            if(jsonArray.length == 0){
                data = '';
                data += '<tr>';
                data += '<td colspan="5">Không có dữ liệu</td>';
                data += '</tr>';
                $("#imageloadmore").hide();
            }
            else{                    
                data = '';                     
                console.log($.each(jsonArray, function(date, hour){   
                showtime_date = '';
                for(i=0; i < hour.length; i++){
                    showtime = jQuery.parseJSON(hour[i]);
                    if(showtime.movie_format == movie_format && showtime.dubbed == dubbed){
                        var showtime_date = date;                         
                    }
                }
                if(showtime_date != ''){
                        data += '<tr>';                

                        //Ngày
                        data += '<td>';                                        
                        data += showtime_date;                
                        data += '</td>';

                        //Giờ chiếu
                        data += '<td>';
        //                console.log(hour);
                        for(i=0; i < hour.length; i++){
                            showtime = jQuery.parseJSON(hour[i]);   
                            console.log(showtime);
                            if(showtime.movie_format == movie_format && showtime.dubbed == dubbed){                        
                                if(showtime.timestamp * 1000 < time.getTime()){                         
                                    data += '<span id="' + showtime.timestamp +'" class="showtime_detail' +' current_timestamp' + '" >' + showtime.hour + '<a href="#" onclick="del_showtime(' + "'" + showtime.timestamp +"','" + showtime.movie_id + "','" + showtime.cinema_id +"'"+ ')" > x </a>' + '</span>';
                                }else{                        
                                    data += '<span id="' + showtime.timestamp +'" class="showtime_detail">' + showtime.hour + '<a href="#" onclick="del_showtime(' + "'" + showtime.timestamp +"','" + showtime.movie_id + "','" + showtime.cinema_id +"'"+ ')" > x </a>' + '</span>';
                                }
                            }
                        }                                                                                                   
                        data += '</td>';     


                        data += '</tr>'; 
                    }
                }));                                 
            }                        
        }  
        $("#content").html(data);
    }
    
    /**
     * del_showtime
     *
     * Delete showtime
     */
    function del_showtime(timestamp, movie_id, cinema_id)
    {        
        if(confirm('Bạn có chắc là muốn xóa giờ chiếu này không?'))
        {
            $.ajax({
                timeout: <?php echo (get_ajax_call_options("tiemout") != NULL) ? get_ajax_call_options("tiemout") : "10000"; ?>,
                type: "POST",
                url: $('#base_url').val() + "showtime/ajax_delete_showtime",
                data: {timestamp: timestamp, movie_id: movie_id, cinema_id:cinema_id},
                beforeSend: (function(){
                    $("#ax_form_result").html("");
                }),
                success: (function(json) {
                    $("#" + timestamp).remove();
                    $(".loading").hide();
                    alert(json.message);
                }),
                complete: (function() {

                }),
                dataType: "json"
            });
        }
        return false;
    }
    
    function insert_crawl_uri_list(jsonArray){    
        console.log(jsonArray);
        var i , j;                
        if(jsonArray != null){
            if(jsonArray.length == 0){
                $("#link_crawl_movie_showtime").hide();
                data = '';
                data += '<tr>';
                data += '<td colspan="5">Không có link crawl lịch chiếu phim</td>';
                data += '</tr>';
                $("#imageloadmore").hide();               
            }
            else{
                $("#link_crawl_movie_showtime").show();
                data = '';                     
                console.log($.each(jsonArray, function(index, crawl_uri){                                                                                                                 
                    
                    data += '<tr id="'+crawl_uri.id+'">';                   
                                
                    //Stt
                    data += '<td>';   
                    data += index + 1;   
                    data += '</td>';
                    
                    //Link
                    data += '<td style="text-align: left">';   
                    data += '<a href="'+crawl_uri.url+'">';                       
                    data += crawl_uri.url; 
                    data += '</a>';
                    data += '</td>';

                    //Xóa
                    data += '<td>'; 
                    data += '<a href="" onclick="return del_link_crawl_showtime(' + "'"+crawl_uri.id+"'" +')">';
                    data += 'Xóa'; 
                    data += '</a>';
                    data += '</td>';     

                    data += '</tr>'; 
                }));                                 
            }                        
        }  
        
        $("#link_showtime_content").html(data);
    }
    
    function del_link_crawl_showtime(id){
        if(confirm('Bạn có chắc là muốn xóa link crawl url?'))
        {                      
            $.ajax({
                type: "POST",
                url: $('#base_url').val() + "showtime/ajax_delete_crawl_uri",
                data: {movie_id: $('#movie_id').val(), cinema_id: $("input:checked").val(), crawl_uri_id: id},               
                success: (function(json) {
                    $("#" + id).remove();
                    $(".loading").hide();
                    alert(json.message);
                }),
                complete: (function() {

                }),
                dataType: "json"
            });
        }
        return false;
    }
    
 /*------------------------------------------Channel Share--------------------------------------------------*/
   
   
    $(".add_link_crawl_showtime").click(function(){           
        $(".progressbar").progressbar( "option", "value", 0 );            
        $("#form_crawl_showtime").dialog("open");
        $("#crawl_cinema_id").val($("input:checked").val());
        return false;
    });

    $("#form_crawl_showtime").dialog({
        width: 500,
        autoOpen: false,
        buttons: {                
            "Submit": function(){
                $( ".progressbar" ).progressbar( "option", "value", 0 );
                $("#form_movie_showtime").submit(); 
                $(".loading").show();
            },
            "Đóng": function() {
                $( ".progressbar" ).progressbar( "option", "value", 0 );
                $("#acx_crawling_result > table > tbody").html('<tr id="acx_crawl_link_wrap" hidden="hidden" />');
                $(this).dialog("close");
                $(".loading").hide();
            }
        }
    });
</script>

